package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.ui.publicui.PayListDialog;
import com.hzcx.app.simplechat.ui.publicui.bean.HxUserInfoBean;
import com.hzcx.app.simplechat.ui.setting.adapter.VipAdapter;
import com.hzcx.app.simplechat.ui.setting.bean.VipInfoBean;
import com.hzcx.app.simplechat.ui.setting.contract.VipContract;
import com.hzcx.app.simplechat.ui.setting.presenter.VipPresenter;
import com.hzcx.app.simplechat.util.UserInfoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 多聊专业版
 */
public class VipActivity extends BaseActivity<VipPresenter> implements VipContract.View {

    @BindView(R.id.iv_bottom)
    ImageView ivBottom;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_card_title)
    TextView tvCardTitle;
    @BindView(R.id.tv_now_version)
    TextView tvNowVersion;
    @BindView(R.id.cons_card)
    ConstraintLayout consCard;
    @BindView(R.id.ll_item)
    LinearLayout llItem;
    @BindView(R.id.tv_chat)
    TextView tvChat;
    @BindView(R.id.tv_pay)
    Button tvPay;
    @BindView(R.id.rv_vip_item)
    RecyclerView rvVipItem;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.ll_chat)
    LinearLayout llChat;

    private List<VipInfoBean.PlusDataBean> itemData;
    private VipAdapter vipAdapter;
    private VipInfoBean vipInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_vip;
    }

    @Override
    public void initPresenter() {
        mPresenter = new VipPresenter();
    }

    @Override
    public void initView() {
        itemData = new ArrayList<>();
        vipAdapter = new VipAdapter(itemData);
        rvVipItem.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvVipItem.setAdapter(vipAdapter);
        rvVipItem.setHasFixedSize(true);
        rvVipItem.setNestedScrollingEnabled(false);

        tvTitle.setText(getResources().getString(R.string.app_name) + "专业版");
        tvCardTitle.setText(getResources().getString(R.string.app_name) + "专业版");
    }

    @Override
    public void initData() {
        mPresenter.getVipInfo(this);
    }

    @OnClick({R.id.tv_chat, R.id.tv_pay})
    public void viewOnClick(View view) {
        if (vipInfoBean == null) {
            showError("未获取到客服信息，请稍后重试");
            return;
        }
        HxUserInfoBean hxUserInfoBean = new HxUserInfoBean();
        hxUserInfoBean.setMyUserId(UserInfoUtil.getUserId());
        hxUserInfoBean.setNickname(vipInfoBean.getNickname());
        hxUserInfoBean.setMember_id(vipInfoBean.getMember_id());
        hxUserInfoBean.setAvatar(vipInfoBean.getAvatar());
        hxUserInfoBean.setHxusername(vipInfoBean.getHxusername());
        switch (view.getId()) {
            case R.id.tv_chat:
                ChatUtil.getInstance().toChatUi(this, hxUserInfoBean);
                break;

            case R.id.tv_pay:
                PayListDialog payListDialog = new PayListDialog(this, hxUserInfoBean);
                payListDialog.show();
                break;
        }
    }

    /**
     * 会员信息获取成功
     *
     * @param data
     */
    @Override
    public void vipResult(VipInfoBean data) {
        try {
            vipInfoBean = data;
            itemData.clear();
            itemData.addAll(data.getPlusData());
            vipAdapter.notifyDataSetChanged();
            if (data.getPlusVersion().equals("plus")) {
                tvNowVersion.setText("专业版");
                tvPay.setText("续费");
            } else {
                tvNowVersion.setText("免费版");
            }
            tvTips.setText("开通专业版，享" + data.getPlusFeaturesCount() + "大特权");
            scrollView.setVisibility(View.VISIBLE);
            llChat.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
